package com.gzyj.flink.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * @author lhj
 */
public class RoadUtils {

    private static final Pattern PATTERN = Pattern.compile("[G-S]\\d*");

    public static int countPattern(String references, Pattern referencePattern) {
        Matcher matcher = referencePattern.matcher(references);
        return Stream.iterate(0, i -> i + 1)
                .filter(i -> !matcher.find())
                .findFirst()
                .get();
    }

    public static String GetRoadLevelByRoadName(String roadName) {
        String roadLevel = "";
        roadLevel = "其他道路";
        if (StringUtils.isEmpty(roadName)) return roadLevel;
        boolean isFlag = false;

        if (!isFlag) {
            if (roadName.indexOf("高速") > 0) {
                roadLevel = "高速公路";
            } else if (roadName.indexOf("国道") > 0) {
                roadLevel = "国道";
            } else if (roadName.indexOf("省道") > 0) {
                roadLevel = "省道";
            } else if (roadName.indexOf("县道") > 0) {
                roadLevel = "县道";
            } else if (roadName.indexOf("乡道") > 0) {
                roadLevel = "乡道";
            } else if (roadName.contains("G")) {
                int lenth = countPattern(roadName, PATTERN);
                if (lenth == 1 || lenth == 2 || lenth == 3 || lenth == 4) {
                    roadLevel = "高速公路";
                }
            } else if (roadName.contains("S")) {
                int lenth = countPattern(roadName, PATTERN);
                if (lenth == 1 || lenth == 2 || lenth == 3 || lenth == 4) {
                    roadLevel = "省道";
                }
            } else {
                int level = isGuoDao(roadName);
                if (level == 1) {
                    roadLevel = "国道";
                } else if (level == 2) {
                    roadLevel = "高速公路";
                } else {
                    if (IsShenDao(roadName)) {
                        roadLevel = "省道";
                    } else {
                        if (isXianDao(roadName)) {
                            roadLevel = "县道";
                        } else {
                            if (isXiangDao(roadName)) {
                                roadLevel = "乡道";
                            }
                        }
                    }

                }
            }
        }

        return roadLevel;
    }

    private static boolean isXiangDao(String roadName) {
        String road = roadName.toLowerCase();
        Pattern r = Pattern.compile("y\\d+");
        return r.matcher(road).find();
    }

    private static boolean isXianDao(String roadName) {
        String road = roadName.toLowerCase();
        Pattern r = Pattern.compile("x\\d+");
        return r.matcher(road).find();
    }

    private static boolean IsShenDao(String roadName) {
        String road = roadName.toLowerCase();
        Pattern r = Pattern.compile("s\\d+");
        return r.matcher(road).find();
    }

    private static int isGuoDao(String roadName) {
        String road = roadName.toLowerCase();
        Pattern r = Pattern.compile("g\\d+");
        if (!r.matcher(road).find())
            return 0;

        Matcher mc = r.matcher(road);
        String result = "";
        int level = 0;
        while (mc.find()) {
            result += mc.group();
            if (result.length() == 3 + 1)
                level = 1;
            else
                level = 2;
            break;
        }
        return level;

    }


}
